/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package service.views;

import com.openbrew.openbrewrf.Administration;
import com.openbrew.openbrewrf.Brews;
import com.openbrew.openbrewrf.views.BrewView;
import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import service.AbstractFacade;
import tools.BrewsTools;
import utils.Calcs;

/**
 *
 * @author dchapman
 */
@Stateless
@Path("com.openbrew.openbrewrf.brewview")
public class BrewViewFacadeREST extends AbstractFacade<BrewView> {
    @PersistenceContext(unitName = "com.openBrew_openBrewRF_war_1.0PU")
    private EntityManager em;

    public BrewViewFacadeREST() {
        super(BrewView.class);
    }

    @POST
    @Override
    @WebMethod(operationName = "create")
    @Consumes({"application/xml", "application/json"})
    public BrewView create(@WebParam(name = "brewview") BrewView entity) {
        entity = super.create(entity);
        return entity;
    }

    @PUT
    @Override
    @Consumes({"application/xml", "application/json"})
    public void edit(BrewView entity) {
        BrewsTools bt = new BrewsTools();
        bt.doUpdateAndCheckForRefactor(em, entity);
    }

    @GET
    @Path("{id}")
    @Produces({"application/xml", "application/json"})
    public BrewView find(@PathParam("id") Integer id) {
        return super.find(id);
    }

    @GET
    @Override
    @Produces({"application/xml", "application/json"})
    public List<BrewView> findAll() {
        return super.findAll();
    }

    @GET
    @Path("{from}/{to}")
    @Produces({"application/xml", "application/json"})
    public List<BrewView> findRange(@PathParam("from") Integer from, @PathParam("to") Integer to) {
        return super.findRange(new int[]{from, to});
    }

    @GET
    @Path("namedQueryRange/{from}/{to}/{str}/{id}")
    @Produces({"application/xml", "application/json"})
    public List<BrewView> findNamedQueryRange(@PathParam("from") Integer from
            , @PathParam("to") Integer to, @PathParam("str") String str
            , @PathParam("id") Integer id) {
        return super.findNamedQueryRange(new int[]{from, to}, str, id);
    }

    @GET
    @Path("namedQueryCount/{str}/{id}")
    @Produces("text/plain")
    public String namedQueryCount(@PathParam("str") String str
            , @PathParam("id") Integer id) {
        return String.valueOf(super.namedQueryCount(str, id, "cId"));
    }
    
    @GET
    @Path("runNamedQueryWithID/{str}/{id}")
    @Override
    @Produces({"application/xml", "application/json"})
    public List<BrewView> runNamedQueryWithID(@PathParam("str") String str
            , @PathParam("id") int id){
        return super.runNamedQueryWithID(str, id);
    }
    
    @GET
    @Path("runNamedQuery/{str}")
    @Override
    @Produces({"application/xml", "application/json"})
    public List<BrewView> runNamedQuery(@PathParam("str") String str){
        return super.runNamedQuery(str);
    }

    @GET
    @Path("count")
    @Produces("text/plain")
    public String countREST() {
        return String.valueOf(super.count());
    }

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }
    
    @GET
    @Path("runQuery/{str}")
    @Override
    @Produces({"application/xml", "application/json"})
    public List<BrewView> runQuery(@PathParam("str") String str){
        return super.runQuery(str);
    }

    @GET
    @Path("getLastGravity/{id}")
    @Produces("text/plain")
    public String lastGravity(@PathParam("id") Integer id) {
        final Brews brew = getBrew(em, id);
        final Administration admin = getAdmin(em);

        if (brew.getBtg() > 1) {
            if (admin.getBPlato()) {
                return Float.toString(brew.getBtg());
            } else {
                return Double.toString(Calcs.platotoSpecificGravity(brew.getBtg()));
            }
        } else if (brew.getSFGrav01() > 1) {
            if (admin.getBPlato()) {
                return Float.toString(brew.getSFGrav01());
            } else {
                return Double.toString(Calcs.platotoSpecificGravity(brew.getSFGrav01()));
            }
        } else {
            if (brew.getFermentationNoteCollection() != null) {
                if (admin.getBPlato()) {
                    return Float.toString(brew.getFermentationNoteCollection().get(brew.getFermentationNoteCollection().size() - 1).getFermNotesGravity());
                } else {
                    return Double.toString(Calcs.platotoSpecificGravity(brew.getFermentationNoteCollection().get(brew.getFermentationNoteCollection().size() - 1).getFermNotesGravity()));
                }
            } else {
                return "1.000";
            }
        }
    }

    @POST
    @Path("createNewBrewSchedule/{beerID}/{volume}/{brewDate}")
    @WebMethod(operationName = "create")
    @Consumes({"application/xml", "application/json"})
    public BrewView createNewBrewSchedule(@PathParam("beerID") int beerID
            , @PathParam("volume") float volume, @PathParam("brewDate") Date brewDate) {
        BrewsTools bt = new BrewsTools();
        return bt.doCreateNewBrewSchedule(em, beerID, volume, brewDate);
    }

    /**
     * ***********************************************************************
     * Database access files.
     ************************************************************************
     */
    private Brews getBrew(final EntityManager en, final int brewID) {
        TypedQuery<Brews> query = en.createQuery(
                "SELECT b FROM Brews b WHERE b.bid = ?1", Brews.class);
        query.setParameter(1, brewID);
        return query.getSingleResult();
    }

    private Administration getAdmin(final EntityManager en) {
        TypedQuery<Administration> query = en.createQuery(
                "SELECT a FROM Administration a WHERE a.aNum = 1", Administration.class);
        return query.getSingleResult();
    }
}
